package com.hyh.baowenlioaworking.service.impl;

import com.hyh.baowenlioaworking.commom.Page;
import com.hyh.baowenlioaworking.mapper.DepartmentMapper;
import com.hyh.baowenlioaworking.model.Department;
import com.hyh.baowenlioaworking.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsPackage: com.hyh.baowenlioaworking.service.impl
 * @ClassName DepartmentServiceImpl
 * @Author: hyhWTX
 * @CreateTime: 2023-07-21  14:05
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> queryAll() {

        return departmentMapper.queryAll();
    }

    @Override
    public int saveDepartment(@RequestBody Department department) {
        if (department.getId() == null) {
            log.info("1111");
            return departmentMapper.addDepartment(department);
        }
        return departmentMapper.updateDepartment(department);
    }


    //    @Override
//    public int deleteDepartment(Integer id) {
//        return departmentMapper.deleteDepartment(id);
//    }
    @Override
    public boolean deleteDepartmentByIdWithCheck(int departmentId) {
        return departmentMapper.deleteDepartmentByIdWithCheck(departmentId) > 0;
    }

    @Override
    public int deleteAll() {
        return departmentMapper.deleteAll();
    }

    @Override
    public Page<Department> findDepartmentByPage(Integer currentPage, Integer size) { //page为当前页
        Map<String, Object> map = new HashMap<>();
        map.put("currentIndex", (currentPage - 1) * size);  //当前索引
        map.put("size", size);
        List<Department> list = departmentMapper.findDepartmentByPage(map);
        Page<Department> page = new Page<>();
        page.setCurrentPage(currentPage);
        page.setSize(size);
        page.setData(list);
        return page;

    }
}
